package com.irdstudio.efp.esb.service.impl.wsd.riskmanagement;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.wsd.riskmanagement.RiskManagementApproveReqBean;
import com.irdstudio.efp.esb.service.bo.resp.wsd.riskmanagement.RiskManagementApproveRespBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.wsd.riskmanagement.RiskManagementApproveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("riskManagementApproveService")
public class RiskManagementApproveServiceImpl implements RiskManagementApproveService {
    private static Logger logger = LoggerFactory.getLogger(RiskManagementApproveServiceImpl.class);

    @Override
    public RiskManagementApproveRespBean applyRiskDecision(RiskManagementApproveReqBean riskManagementApproveReqBean) throws ESBException {
        logger.info("======>调用网商贷风控审批接口【" + riskManagementApproveReqBean.getQryRptsInfArry()[0].getRqsIdmptntID() + "_30220001_08】开始<======");
        RiskManagementApproveRespBean riskManagementApproveRespBean = null;
        TraceUtil.clear();
        try {
            EsbRespServiceBean recvBean = ESBClientFactory.buildClient()
                    .withBody(riskManagementApproveReqBean)
                    .withTradeNo(ESBConstant.TRADE_NO_30220001)
                    .withScene(ESBConstant.SCENE_NO_08)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                riskManagementApproveRespBean = recvBean.getBody(RiskManagementApproveRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用网商贷风控审批接口【30220001_08】结束，返回信息：" + JSONObject.toJSONString(riskManagementApproveRespBean));
        } catch (Exception e) {
            logger.error("调用网商贷风控审批接口【30220001_08】出现异常：" + e.getMessage());
            throw new ESBException("调用网商贷风控审批接口【30220001_08】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用网商贷风控审批接口【" + riskManagementApproveReqBean.getQryRptsInfArry()[0].getRqsIdmptntID() + "_30220001_08】结束<======");
        }
        return riskManagementApproveRespBean;
    }

}